package com.niit.controller;

import com.niit.entity.User;
import com.niit.mapper.UserMapper;
import com.niit.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Arrays;

public class UserController {
    public static void main(String[] args) {

        SqlSession sqlSession = MybatisUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = userMapper.getUserById("Anna");

        System.out.println(user.getUserHobbies());
        sqlSession.close();
        // -----------------------添加学生信息---------------------------------------
        SqlSession insertSqlSession = MybatisUtil.getSqlSession();

        userMapper = insertSqlSession.getMapper(UserMapper.class);

        User insertUser = new User();
        insertUser.setUserId("niit-hanliang");
        insertUser.setUserPwd("654321");
        insertUser.setUserHobbies(Arrays.asList("reading", "swimming","Gamming"));

        userMapper.insertUser(insertUser);
        insertSqlSession.commit(); // 提交事务

        insertSqlSession.close();

        System.out.println("添加成功！");
    }
}
